<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace longbingcore\tools;

class LongbingFile
{
    public static function downloadFile($url, $folderPath, $fileName = NULL, $ext = ".zip")
    {
        set_time_limit(86400);
        $target_dir = $folderPath . "";
        if(!is_dir($target_dir)) {
            mkdir($target_dir, 511, true);
        }
        $newfname = $fileName ? $fileName . $ext : date("Ymd") . rand(1000, 10000000) . uniqid() . $ext;
        $newfname = $target_dir . $newfname;
        $file = @fopen($url, "rb");
        if($file) {
            $newf = fopen($newfname, "wb");
            if($newf) {
                while (!feof($file)) {
                    fwrite($newf, fread($file, 8192), 8192);
                }
            }
            fclose($file);
            if($newf) {
                fclose($newf);
            }
            return $newfname;
        }
        return false;
    }
    public static function unzip($filename, $toFilepath, $password = NULL)
    {
        $zip = new \ZipArchive();
        $res = $zip->open($filename);
        if($res === true) {
            $password && $zip->setPassword($password);
            $zip->extractTo($toFilepath);
            $zip->close();
        }
        return true;
    }
    public function downLocationServiceFile($fileName, $fancyName = "", $forceDownload = true, $speedLimit = 0, $contentType = "")
    {
        if(!is_readable($fileName)) {
            header("HTTP/1.1 404 Not Found");
            return false;
        }
        $fileStat = stat($fileName);
        $lastModified = $fileStat["mtime"];
        $md5 = md5($fileStat["mtime"] . "=" . $fileStat["ino"] . "=" . $fileStat["size"]);
        $etag = "\"" . $md5 . "-" . crc32($md5) . "\"";
        header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastModified) . " GMT");
        header("ETag: " . $etag);
        if(isset($_SERVER["HTTP_IF_MODIFIED_SINCE"]) && $lastModified <= strtotime($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
            header("HTTP/1.1 304 Not Modified");
            return true;
        }
        if(isset($_SERVER["HTTP_IF_UNMODIFIED_SINCE"]) && strtotime($_SERVER["HTTP_IF_UNMODIFIED_SINCE"]) < $lastModified) {
            header("HTTP/1.1 304 Not Modified");
            return true;
        }
        if(isset($_SERVER["HTTP_IF_NONE_MATCH"]) && $_SERVER["HTTP_IF_NONE_MATCH"] == $etag) {
            header("HTTP/1.1 304 Not Modified");
            return true;
        }
        if($fancyName == "") {
            $fancyName = basename($fileName);
        }
        if($contentType == "") {
            $contentType = "application/octet-stream";
        }
        $fileSize = $fileStat["size"];
        $contentLength = $fileSize;
        $isPartial = false;
        if(isset($_SERVER["HTTP_RANGE"]) && preg_match("/^bytes=(\\d*)-(\\d*)\$/", $_SERVER["HTTP_RANGE"], $matches)) {
            list($startPos, $endPos) = $matches;
            if($startPos == "" && $endPos == "") {
                return false;
            }
            if($startPos == "") {
                $startPos = $fileSize - $endPos;
                $endPos = $fileSize - 1;
            } elseif($endPos == "") {
                $endPos = $fileSize - 1;
            }
            $startPos = $startPos < 0 ? 0 : $startPos;
            $endPos = $fileSize - 1 < $endPos ? $fileSize - 1 : $endPos;
            $length = $endPos - $startPos + 1;
            if($length < 0) {
                return false;
            }
            $contentLength = $length;
            $isPartial = true;
        }
        if($isPartial) {
            header("HTTP/1.1 206 Partial Content");
            header("Content-Range: bytes " . $startPos . "-" . $endPos . "/" . $fileSize);
        } else {
            header("HTTP/1.1 200 OK");
            $startPos = 0;
            $endPos = $contentLength - 1;
        }
        header("Pragma: cache");
        header("Cache-Control: public, must-revalidate, max-age=0");
        header("Accept-Ranges: bytes");
        header("Content-type: " . $contentType);
        header("Content-Length: " . $contentLength);
        if($forceDownload) {
            header("Content-Disposition: attachment; filename=\"" . rawurlencode($fancyName) . "\"");
        }
        header("Content-Transfer-Encoding: binary");
        $bufferSize = 2048;
        if($speedLimit != 0) {
            $packetTime = floor($bufferSize * 1000000 / $speedLimit);
        }
        $bytesSent = 0;
        $fp = fopen($fileName, "rb");
        fseek($fp, $startPos);
        while ($bytesSent < $contentLength && !feof($fp) && connection_status() == 0) {
            if($speedLimit != 0) {
                list($usec, $sec) = explode(" ", microtime());
                $outputTimeStart = (double) $usec + (double) $sec;
            }
            $readBufferSize = $contentLength - $bytesSent < $bufferSize ? $contentLength - $bytesSent : $bufferSize;
            $buffer = fread($fp, $readBufferSize);
            echo $buffer;
            ob_flush();
            flush();
            $bytesSent += $readBufferSize;
            if($speedLimit != 0) {
                list($usec, $sec) = explode(" ", microtime());
                $outputTimeEnd = (double) $usec + (double) $sec;
                $useTime = ((double) $outputTimeEnd - (double) $outputTimeStart) * 1000000;
                $sleepTime = round($packetTime - $useTime);
                if(0 < $sleepTime) {
                    usleep($sleepTime);
                }
            }
        }
        return true;
    }
    public static function createDir($path, $mode = "0777")
    {
        if(!is_dir($path)) {
            return mkdir($path, $mode);
        }
    }
    public static function deldir($path)
    {
        if(is_dir($path)) {
            $p = scandir($path);
            foreach ($p as $val) {
                if($val != "." && $val != "..") {
                    if(is_dir($path . $val)) {
                        LongbingFile::deldir($path . $val . "/");
                        @rmdir($path . $val . "/");
                    } else {
                        unlink($path . $val);
                    }
                }
            }
        }
    }
}

?>